package com.android.qjpmc.application_module.home.fragment;

import android.os.Handler;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.android.qjpmc.R;
import com.android.qjpmc.bean.SiteStationBean;
import com.android.qjpmc.constant.Constant;
import com.android.qjpmc.data_module.PwrDataModule;
import com.android.qjpmc.data_module.PwrDevice;
import com.android.qjpmc.data_module.PwrStation;
import com.android.qjpmc.databinding.FragmentHistoryPageBinding;
import com.android.qjpmc.databinding.ItemStationBinding;
import com.android.qjpmc.databinding.ItemStationDeviceBinding;
import com.android.qjpmc.databinding.ItemStationOneBinding;
import com.android.qjpmc.manager.IntentManager;
import com.android.qjpmc.application_module.home.contract.HistoryPageContract;
import com.android.qjpmc.application_module.home.viewModel.HistoryPageViewModel;
import com.android.qjpmc.widget.databindingadapter.BaseBindingItemPresenter;
import com.android.qjpmc.widget.databindingadapter.BaseDataBindingDecorator;
import com.android.qjpmc.widget.databindingadapter.SingleTypeBindingAdapter;
import com.android.qjpmc.widget.mvvm.factory.CreateViewModel;
import com.android.qjpmc.widget.mvvm.view.BaseMVVMFragment;

import java.util.ArrayList;
import java.util.List;

@CreateViewModel(HistoryPageViewModel.class)
public class HistoryPageFragment extends BaseMVVMFragment<HistoryPageViewModel, FragmentHistoryPageBinding> implements HistoryPageContract.View, BaseBindingItemPresenter<SiteStationBean> {
    @Override
    public int getLayoutId() {
        return R.layout.fragment_history_page;
    }

    ArrayList<PwrStation> pwrStations = null;

    PwrStation GetStation(int StationListIndex) {
        if (pwrStations == null) {
            return null;
        }
        return pwrStations.get(StationListIndex);
    }

    @Override
    public void initView() {
        //todo 胖胖修改 直接网络拿数据
        mViewModel.getStationList();

        PwrDataModule.getInstance().SetHistoryFragement(this);

    }

    @Override
    public void initData() {
        m_handler = new Handler();

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onItemClick(int position, SiteStationBean itemData) {
        IntentManager.toHistoryDataActivity(mActivity, itemData.company_id,itemData.company_name);

      /*  PwrStation Station = GetStation(position);
        PwrDataModule.getInstance().SetCurrentStationNode(Station);
        if (Station != null)
        {
            IntentManager.toTransformerSubstationHistoryDataActivity(mActivity);
            //onTransformerSubstation();
        }*/
    }

    //变电站
    @Override
    public void onTransformerSubstation() {
    }  //变电站

    @Override
    public void onNotice() {
        IntentManager.toApprovalTicketActivity(mActivity);
    }

    //历史数据
    @Override
    public void onHistoryData() {

    }

    @Override
    public void returnSiteStationData(List<SiteStationBean> siteStationBeans) {
        mBinding.setView(this);
        pwrStations = PwrDataModule.getInstance().GetAllStationList();
        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(mActivity, siteStationBeans, R.layout.item_station_one);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter.setItemDecorator(new BaseDataBindingDecorator<SiteStationBean, ItemStationOneBinding>() {
            @Override
            public void decorator(ItemStationOneBinding binding, int position, int viewType, SiteStationBean data) {
                // ArrayList<PwrDevice> pwrDevices = PwrDataModule.getInstance().GetDeviceList(data.getM_ID());
                binding.tvDeviceNumber.setText("电柜数量： 电柜的数据量啊");


            }
        });
    }

    private Handler m_handler = null;

    Runnable m_UpdateUIRunnable = new Runnable() {
        @Override
        public void run() {
            returnSiteStationData(PwrDataModule.getInstance().GetStaionBeanList());
        }
    };

    public int UpdateStationListUI() {
        if (m_handler == null) {
            return Constant.GEN_ERROR;
        }
        m_handler.post(m_UpdateUIRunnable);
        return Constant.NO_ERROR;
    }
}
